unit LoginDlg;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Buttons,
  Data.DB, Data.Win.ADODB, Datasnap.DBClient, MainFrm, uBaseIntf, TCPClient, MemBuffer, ContextFactory;

type
  TDlgLogin = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    edtHost: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    edtAccount: TEdit;
    edtPassword: TEdit;
    btnLogin: TBitBtn;
    btnCancle: TBitBtn;
    ClientDataSet1: TClientDataSet;
    lblMessage: TLabel;
    procedure btnLoginClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure FClientFCloseSocket(FChannel: TObject);
    function FClientFNewClientObj: TIClientContext;
    procedure FClientFOpenSocket(FChannel: TObject);
    procedure FClientFRecvSocket(FChannel: TObject; FIBuffer: TIBuffer;
      dwIoSize: Cardinal);
    procedure FClientFSendSocket(FChannel: TObject; FIBuffer: TIBuffer;
      dwIoSize: Cardinal);
    procedure OutputMessage(Sender: TObject; const Value: String; MsgLevel: TMsgLevelOption);
  end;

implementation

{$R *.dfm}

uses Protocol;

procedure TDlgLogin.btnLoginClick(Sender: TObject);
var
  Account: String;
begin
  ModalResult := mrCancel;
  try
    Screen.Cursor := crHourGlass;
    DM.Host := Copy(edtHost.Text, 1, Pos(':', edtHost.Text) - 1);
    DM.Port := StrToIntDef(Copy(edtHost.Text, Pos(':', edtHost.Text) + 1, Length(edtHost.Text)), 211);
    DM.BuffCount := 100;
    DM.ThreadCount := 1;
    if DM.Start then
      ModalResult := mrOk;
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TDlgLogin.FClientFCloseSocket(FChannel: TObject);
begin

end;

function TDlgLogin.FClientFNewClientObj: TIClientContext;
begin
  Result := TIClientContext.Create(DM.TCPEngine);
end;

procedure TDlgLogin.FClientFOpenSocket(FChannel: TObject);
begin

end;

procedure TDlgLogin.FClientFRecvSocket(FChannel: TObject; FIBuffer: TIBuffer;
  dwIoSize: Cardinal);
var
  FSendBuffer: TIBuffer;
begin
  with (FChannel as TIClientContext) do
  begin
    FSendBuffer := GetFreeDIBuffer(IO_WSA_SEND);
    if FSendBuffer <> nil then
    begin
      FSendBuffer.CreatePackage(FIBuffer.GetBuffer, FIBuffer.GetUsed);
      SendStream(FSendBuffer);
    end;
  end;
end;

procedure TDlgLogin.FClientFSendSocket(FChannel: TObject; FIBuffer: TIBuffer;
  dwIoSize: Cardinal);
begin

end;

procedure TDlgLogin.FormCreate(Sender: TObject);
begin
  DM := TBaseClient.Create(Self);
  DM.FNewClientObj := FClientFNewClientObj;
  DM.FOpenSocket  := FClientFOpenSocket;
  DM.FCloseSocket := FClientFCloseSocket;
  DM.FSendSocket  := FClientFSendSocket;
  DM.FRecvSocket  := FClientFRecvSocket;
end;

procedure TDlgLogin.OutputMessage(Sender: TObject; const Value: String;
  MsgLevel: TMsgLevelOption);
var
  str: String;
begin
  str := Value;
  if Copy(str, 1, 3) = '!!!' then
    begin
      Screen.Cursor := crHourGlass;
      lblMessage.Font.Color := clRed;
      lblMessage.Caption := Copy(str, 4, Length(str));
    end
  else
    begin
      if lblMessage.Font.Color = clRed then
      begin
        Screen.Cursor := crDefault;
        ShowMessage(lblMessage.Caption);
        lblMessage.Font.Color := clWindowText;
      end;
      lblMessage.Caption := str;
    end;
  Self.Update;
end;

end.
